package org.xxmi.thread;

class PrintNum1 implements Runnable {

    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().getName() + ":" + i);
        }
    }
}

/**
 * 此程序存在线程安全问题：打印车票时，出现重票，错票
 */
class Window implements Runnable {
    int ticket = 100;

    public void run() {
        while (true) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " 售票：" + ticket--);
            if (ticket < 0) {
                break;
            }
        }
    }
}
class TestThe{
    synchronized static void syah3(){

    }
}

public class TestThread1 {
    public static void main(String[] args) {
//        PrintNum1 p1 = new PrintNum1();
//        Thread t1 = new Thread(p1);
//        t1.start();
//
//        Thread t2 = new Thread(p1);
//        t2.start();

        Window window = new Window();
        Thread t1 = new Thread(window);
        Thread t2 = new Thread(window);
        Thread t3 = new Thread(window);

        t1.start();
        t2.start();
        t3.start();
    }
}
